package com.fancy.learncenter.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.adapter.PingLunAdapter;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.CommentsDataBean;
import com.fancy.learncenter.bean.TopDataBean;
import com.fancy.learncenter.bean.TzDetailDataBean;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.Utils;
import com.fancy.learncenter.utils.WXShareUtil;
import com.fancy.learncenter.view.PopupWindowSendDiscuss;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TzDetailActivity extends BaseActivity {

    ArrayList<String> mData;


    SimpleDraweeView user_icon;
    TextView name;
    TextView time;
    TextView view_num;
    TextView tz_title;
    TextView praise_num;
    TextView praise_bt;
    TextView discuss_num;
    //    RecyclerView pic_recycleView;
    WebView webView;

    @Bind(R.id.recycleView)
    RecyclerView recycleView;
    @Bind(R.id.shoucang)
    LinearLayout shoucang;
    @Bind(R.id.sub_discuss)
    TextView subDiscuss;


    PopupWindowSendDiscuss popupWindowSendDiscuss;
    @Bind(R.id.shoucang_icon)
    ImageView shoucangIcon;

    // true是评论， false 评论回复
    private boolean discussTz = true;

    private int curPosition = 0;

    private String commentId;

    String forumId = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tz_detail, getResources().getColor(R.color.toolbar_color));
        ButterKnife.bind(this);
        initToolbar("精华贴");


        forumId = getIntent().getStringExtra("wdListDataBean");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        initView();


        popupWindowSendDiscuss = new PopupWindowSendDiscuss(this, recycleView);

        popupWindowSendDiscuss.setSendDisCallBack(new PopupWindowSendDiscuss.SendDisCallBack() {
            @Override
            public void sendDiscuss() {

                if (Utils.isFastClick()) {
                    if (discussTz) {
                        tZComment();
                    } else {
                        tZCommentReply();
                    }
                }

            }
        });


        getData();
        tzCommenLists();
    }

    ImageView wx_icon;
    ImageView wx_friend;

    String shareUrlPic = "";
    String shareLinkUrl = "";
    String shareTitle = "";
    String shareContent = "";

    ImageView zan;

    private void initView() {
        View headerView = LayoutInflater.from(this).inflate(R.layout.header_tz_detail, null);
        headerView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        webView = (WebView) headerView.findViewById(R.id.webView);

        user_icon = (SimpleDraweeView) headerView.findViewById(R.id.user_icon);
        user_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TzDetailActivity.this, PersonInfoActivity.class);
                intent.putExtra("userId", "" + user_id);
                startActivity(intent);
            }
        });


        tz_title = (TextView) headerView.findViewById(R.id.tz_title);
        discuss_num = (TextView) headerView.findViewById(R.id.discuss_num);


        name = (TextView) headerView.findViewById(R.id.name);

        time = (TextView) headerView.findViewById(R.id.time);
        view_num = (TextView) headerView.findViewById(R.id.view_num);

        wx_icon = headerView.findViewById(R.id.wx_icon);
        wx_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WXShareUtil.getInstance(TzDetailActivity.this).shareWebPage(shareUrlPic, shareTitle, shareContent, shareLinkUrl, false);
            }
        });

        wx_friend = headerView.findViewById(R.id.wx_friend);
        wx_friend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WXShareUtil.getInstance(TzDetailActivity.this).shareWebPage(shareUrlPic, shareTitle, shareContent, shareLinkUrl, true);
            }
        });

        zan = headerView.findViewById(R.id.zan);
        zan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                top();
            }
        });


        praise_num = (TextView) headerView.findViewById(R.id.praise_num);


        praise_bt = (TextView) headerView.findViewById(R.id.praise_bt);

        praise_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (mfollow) {
                    cancelFollow();
                } else {
                    doFollow();
                }

            }
        });


        initRecycle(headerView);

    }

    private void initRecycle(View view) {
        ArrayList mData = new ArrayList<>();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);
        adapter = new PingLunAdapter(this, mData);
        recycleView.setAdapter(adapter);
        adapter.setHeaderView(view, recycleView);

        adapter.setOnClickCallBack(new PingLunAdapter.OnClickCallBack() {
            @Override
            public void OnClick(int position) {
                discussTz = false;
                curPosition = position;
                commentId = listDataBean.get(position).getId();

                popupWindowSendDiscuss.showPop();
            }
        });
    }

    PingLunAdapter adapter;


    private void getData() {
        Map map = new HashMap();
        map.put("forumId", forumId);

        HttpMehtod.getInstance().tZDetail(map, new IdeaObserver<BaseDataBean<TzDetailDataBean>>(this, true) {


            @Override
            public void onSuccess(BaseDataBean<TzDetailDataBean> tzDetailDataBeanBaseDataBean) {

                shareUrlPic = tzDetailDataBeanBaseDataBean.getData().getShare_img();
                shareContent = tzDetailDataBeanBaseDataBean.getData().getShare_content();
                shareLinkUrl = tzDetailDataBeanBaseDataBean.getData().getShare_link();
                shareTitle = tzDetailDataBeanBaseDataBean.getData().getShare_title();

                user_id = tzDetailDataBeanBaseDataBean.getData().getUser_id();
                user_icon.setImageURI(tzDetailDataBeanBaseDataBean.getData().getImgsrc());
                tz_title.setText(tzDetailDataBeanBaseDataBean.getData().getTitle());
                name.setText(tzDetailDataBeanBaseDataBean.getData().getNick_name());
                time.setText(tzDetailDataBeanBaseDataBean.getData().getAdd_time());
                webView.loadDataWithBaseURL(null, Utils.getHtmlData(tzDetailDataBeanBaseDataBean.getData().getContent()), "text/html", "utf-8", null);
                praise_num.setText(tzDetailDataBeanBaseDataBean.getData().getTop_num() + "");

                view_num.setText(tzDetailDataBeanBaseDataBean.getData().getView_num());

                discuss_num.setText(tzDetailDataBeanBaseDataBean.getData().getComment_num() + "回帖");

                if (tzDetailDataBeanBaseDataBean.getData().getIs_follow() == 0) {

                    mfollow = false;
                    praise_bt.setBackgroundResource(R.drawable.follow_bg);
                    praise_bt.setText("+关注");
                } else {
                    mfollow = true;
                    praise_bt.setText("-已关注");
                    praise_bt.setBackgroundResource(R.drawable.follow_cancer_bg);

                }

                if (tzDetailDataBeanBaseDataBean.getData().getIs_collect() == 0) {
                    mCollect = false;
                    shoucangIcon.setImageResource(R.mipmap.ic_shoucang_gray);
                } else {
                    mCollect = true;
                    shoucangIcon.setImageResource(R.mipmap.ic_shoucangdown);
                }

                topNum = tzDetailDataBeanBaseDataBean.getData().getTop_num();

                if (tzDetailDataBeanBaseDataBean.getData().getIs_top() == 0) {

                    zan.setImageResource(R.mipmap.ic_zan);

                } else {

                    zan.setImageResource(R.mipmap.ic_zan_s);

                }

            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }

    private void tZComment() {
        Map map = new HashMap();
        map.put("forumId", forumId);
        map.put("content", popupWindowSendDiscuss.getDiscuss());

        HttpMehtod.getInstance().tZComment(map, new IdeaObserver<BaseDataBean>() {
            @Override
            public void onSuccess(BaseDataBean listBaseDataBean) {
                tzCommenLists();

                ToastUtil.show("评论成功");
                popupWindowSendDiscuss.dismiss();
            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }

    int topNum;

    private void tZCommentReply() {
        Map map = new HashMap();
        map.put("forumId", forumId);
        map.put("content", popupWindowSendDiscuss.getDiscuss());
        map.put("commentId", commentId);

        HttpMehtod.getInstance().tZCommentReply(map, new IdeaObserver() {
            @Override
            public void onSuccess(BaseDataBean listBaseDataBean) {

                ToastUtil.show("评论成功");
                popupWindowSendDiscuss.dismiss();
                tzCommenLists();
            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }

    List<CommentsDataBean> listDataBean = new ArrayList<>();

    private void tzCommenLists() {
        Map map = new HashMap();
        map.put("forumId", forumId + "");

        HttpMehtod.getInstance().tzCommenLists(map, new IdeaObserver<BaseDataBean<List<CommentsDataBean>>>() {

            @Override
            public void onSuccess(BaseDataBean<List<CommentsDataBean>> listBaseDataBean) {

                if (listBaseDataBean.getData() != null) {
                    listDataBean = listBaseDataBean.getData();
                    adapter.itemDatas.clear();
                    adapter.itemDatas.addAll((ArrayList<CommentsDataBean>) listBaseDataBean.getData());

//                    recycleView.scrollToPosition(3);

//                    recycleView.scrollToPosition(1);

//                    if (discussTz) {
//
//                    } else {
//                        adapter.notifyItemChanged(curPosition);
//                    }

                    adapter.notifyItemRangeChanged(1, listBaseDataBean.getData().size());
                }

            }

            @Override
            public void onFail(int errorCode) {

            }
        });

    }


    String user_id;


    //未关注
    boolean mfollow = false;

    private void doFollow() {
        Map map = new HashMap();
        map.put("tUserId", user_id);

        HttpMehtod.getInstance().doFollow(map, new IdeaObserver<BaseDataBean>(this, true) {

            @Override
            public void onSuccess(BaseDataBean listBaseDataBean) {
                mfollow = true;
                praise_bt.setText("-已关注");
                praise_bt.setBackgroundResource(R.drawable.follow_cancer_bg);
            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }


    private void top() {
        Map map = new HashMap();
        map.put("forumId", forumId + "");

        HttpMehtod.getInstance().top(map, new IdeaObserver<BaseDataBean<TopDataBean>>(this, true) {


            @Override
            public void onSuccess(BaseDataBean<TopDataBean> topDataBeanBaseDataBean) {

                String status = topDataBeanBaseDataBean.getData().getStatus();
                if (status.equals("top")) {
                    topNum++;
                    praise_num.setText(topNum + "");
                    zan.setImageResource(R.mipmap.ic_zan_s);
                } else {
                    topNum--;
                    praise_num.setText(topNum + "");
                    zan.setImageResource(R.mipmap.ic_zan);
                }

            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }

    private void cancelFollow() {
        Map map = new HashMap();
        map.put("tUserId", user_id);

        HttpMehtod.getInstance().cancelFollow(map, new IdeaObserver<BaseDataBean>(this, true) {

            @Override
            public void onSuccess(BaseDataBean listBaseDataBean) {
                mfollow = false;
                praise_bt.setText("+关注");
                praise_bt.setBackgroundResource(R.drawable.follow_bg);

            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }

    boolean mCollect = false;

    private void collect() {
        Map map = new HashMap();
        map.put("relationId", forumId + "");
        map.put("type", "5");

        HttpMehtod.getInstance().collect(map, new IdeaObserver<BaseDataBean>() {


            @Override
            public void onSuccess(BaseDataBean baseDataBean) {
                mCollect = true;
                shoucangIcon.setImageResource(R.mipmap.ic_shoucangdown);
            }

            @Override
            public void onFail(int errorCode) {

            }
        });

    }

    private void cancelCollect() {
        Map map = new HashMap();
        map.put("relationId", forumId + "");
        map.put("type", "5");

        HttpMehtod.getInstance().cancelCollect(map, new IdeaObserver<BaseDataBean>() {


            @Override
            public void onSuccess(BaseDataBean baseDataBean) {
                mCollect = false;
                shoucangIcon.setImageResource(R.mipmap.ic_shoucang_gray);
            }

            @Override
            public void onFail(int errorCode) {

            }
        });

    }

    @OnClick({R.id.shoucang, R.id.sub_discuss})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.shoucang:

                if (mCollect) {
                    cancelCollect();
                } else {
                    collect();
                }

                break;
            case R.id.sub_discuss:
                discussTz = true;
                popupWindowSendDiscuss.showPop();
                break;
        }
    }
}
